<?php
include '../include/functions.php';
//set session save path
  session_save_path("../$session_path");
  // Initialize a session. 
  session_start(  );
            //session_destroy();             
  $count++;                         
  $sessionId = session_id();
//Connect To Database
dbConnect();
if($userID != "")
{
	$SQL = "SELECT Username FROM NFLUsers WHERE ID=$userID";
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	$myrow = mysql_fetch_array($result);
	$username = $myrow["Username"];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/MAIN.dwt.php" codeOutsideHTMLIsLocked="false" -->
  <head>
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>JoshHughes.com NFL Football Pool</title>
    <!-- InstanceEndEditable -->
	<link rel="stylesheet" href="../css/football.css" type="text/css">
    <script src="../scripts/prototype.js" type="text/javascript"></script>
	<script src="../scripts/scriptaculous.js" type="text/javascript"></script>
	<script type="text/javascript">
	// Dynamic Iframe loader
	function loadIframe(theFrame, theURL) {
		document.getElementById(theFrame).src=theURL;
	}
	
	// resizes Iframe according to content
	function resizeMe(obj){ 
	docHeight = mainContent.document.body.scrollHeight
	 obj.style.height = docHeight + 'px'
	 } 
	</script>
    <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("../layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- InstanceBeginEditable name="Contents" -->
			<?php

				$js="function smilie(emote) {\n document.topic.comments.value += emote;\n document.topic.comments.focus();\n}\n";
				require_once("config.inc.php");
				require_once("mysql.php");
				require_once("tags.php");
				require_once("censorship.php");
				if (isset($_POST['comments'])) {
				  $topic=strip_tags($_POST[topic]);
				  $comments=strip_tags($_POST[comments],$allowedtags);
				  foreach ($banlist as $word) {
					$count=strlen($word);
					$i=0;
					while ($i < $count) {
					  $replace .= "*";
					  $i++;
					}
					$comments=str_replace($word,$replace,$comments);
					unset($replace,$count);
				  }
				  foreach ($banlist as $word) {
					$count=strlen($word);
					$i=0;
					while ($i < $count) {
					  $replace .= "*";
					  $i++;
					}
					$topic=str_replace($word,$replace,$topic);
					unset($replace,$count);
				  }
				  $date=time();
				  
				  if (strlen($topic) > 0) {
					if (strlen($comments) > 0) {
					  $Username=$username;
					  $sql="INSERT INTO tinybb_topics SET author='$Username', name='$topic'";
					  if (mysql_query($sql)) {
						$topicid=mysql_insert_id();
						$sql_post="INSERT INTO tinybb_posts SET topicid='$topicid', date='$date', author='$Username', text='$comments'";
						if (mysql_query($sql_post)) {
						  $postid=mysql_insert_id();
						  $sql_last="UPDATE tinybb_topics SET lastpost='$date', lastpostid='$postid' WHERE id='$topicid'";
						  mysql_query($sql_last);
						  header("Location: topic.php?id=$topicid");
							?>
							<SCRIPT LANGUAGE="JavaScript">window.location="topic.php?id=<?= $topicid; ?>";</script>
							<?
						}
						else {
						  echo "<p><b>There has been a problem processing your request.</b></p>\n<p>Please <a href=\"javascript:history.go(-1)\">go back</a> and try again.</p>\n";
						}
					  }
					  else {
						echo "<p><b>There has been a problem processing your request.</b></p>\n<p>Please <a href=\"javascript:history.go(-1)\">go back</a> and try again.</p>\n";
					  }
					}
					else {
					  require_once("headers.php");
					  echo "<p><b>You did not enter a topic post.</b></p>\n<p>Please <a href=\"javascript:history.go(-1)\">go back</a> and try again.</p>\n";
					  require_once("footers.php");
					}
				  }
				  else {
					require_once("headers.php");
					echo "<p><b>You did not enter a topic.</b></p>\n<p>Please <a href=\"javascript:history.go(-1)\">go back</a> and try again.</p>\n";
					require_once("footers.php");
				  }
				}
				else {
				  require_once("headers.php");
				  if ($username != "") {
					echo "<form name=\"topic\" action=\"newtopic.php\" method=\"post\">
					<p><b><label for=\"topic\">Topic:</label></b><br /><input type=\"text\" size=\"50\" name=\"topic\" id=\"topic\" maxlength=\"30\" /></p>
					<p>
						<b class=\"nonpost\"><label for=\"comments\">Post a message:</label></b><br />
						The only allowed html tags are $allowedtagshtml
						<br /><br /><b class=\"nonpost\">Add smilies:</b><br />\n";
					foreach ($smilies as $emote) {
					  echo "		<a href=\"#post\" onclick=\"smilie(' [$emote] '); return false;\"><img border=\"0\" src=\"_images/smilies/$emote.gif\" width=\"16\" height=\"16\" alt=\"$emote\" /></a>&nbsp; \n";
					}
					echo "	</p>
					<textarea name=\"comments\" id=\"comments\" cols=\"52\" rows=\"8\">$_POST[comments]</textarea><br /><br />
					<input type=\"image\" class=\"clear\" src=\"_images/form_submit.gif\" />
				</form>\n";
				  }
				  else {
					require_once("login_form.php");
				  }
				  require_once("footers.php");
				}
				?>

			<!-- InstanceEndEditable -->
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("../layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("../layout/footer.html"); ?>
			
		</div>
</div>
</body>
<!-- InstanceEnd --></html>